﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Lucene.Net.Analysis;
using System.IO;
using System.Text;
using JingBaiHui.Common;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using HouseResourceAnalysis.Models;
using System.Data;
using HouseResourceAnalysis.Target;

namespace HouseResource.Test
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void GetHouseTest()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("aa");
            var dr = dt.NewRow();
            dr["aa"] = "asdfsdf";
            dt.Rows.Add(dr);
            ExcelHelper.ExportExcel(dt);
            string url = "http://cs.fang.anjuke.com/loupan/238112.html";
            //var temp = GetHouse(url);
        }
        [TestMethod]
        public void GetTest()
        {
            Dictionary<string, string> areaUrls = new Dictionary<string, string>();
            string domianUrl = "http://www.dianping.com";
            string url = "http://www.dianping.com/shopall/344/0#BDBlock";
            var html = HttpHelper.Request(url, null, RestSharp.Method.GET);
            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            doc.LoadHtml(html);
            var mainNode = doc.DocumentNode.SelectSingleNode("//*[@id=\"top\"]/div[6]/div/div[2]");
            var areaNodes = mainNode.ChildNodes.Where(t => t.InnerHtml.Contains("class=\"Bravia\"")).ToList();
            for (int i = 0; i < areaNodes.Count; i++)
            {
                var areaNode = areaNodes[i];
                
                var ddNode = areaNode.ChildNodes.FirstOrDefault(t => t.Name.Equals("dd"));
                if (ddNode != null)
                {
                    var ulNode = ddNode.ChildNodes.FirstOrDefault(t => t.Name.Equals("ul"));
                    if (ulNode != null)
                    {
                        var liNodes = ulNode.ChildNodes.Where(t => t.Name.Equals("li")).ToList();
                        for (int h = 0; h < liNodes.Count; h++)
                        {
                            var aNode = liNodes[h].ChildNodes.FirstOrDefault(t => t.Name.Equals("a"));
                            var href = domianUrl+aNode.Attributes["href"].Value;
                            var areaName = areaNode.ChildNodes[1].InnerText + "-" + aNode.InnerText;
                             areaUrls.Add(areaName, href);
                        }
                    }
                }
                // if (node.InnerHtml.Contains(""))
            }

        }
        [TestMethod]
        public void GetPageTest()
        {
            int total = 0;
            int pageSize = 50;
            int pageCount = 0;
            //List<string> detailLinks = new List<string>();

            string url = "http://cs.fang.anjuke.com/loupan/all/d123";
            var html = HttpHelper.Request(url + "/", null, RestSharp.Method.GET);
            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            doc.LoadHtml(html);
            var totalNode = doc.DocumentNode.SelectSingleNode("//*[@id=\"container\"]/div[2]/div[1]/div[4]/span/em");
            int.TryParse(totalNode.InnerText.Trim(), out total);
            pageCount = total / pageSize + 1;

            List<House> houseList = new List<House>();

            for (int i = 1; i <= pageCount; i++)
            {
                string link = $"{url}_p{i}/";
                var links = GetDetailLinks(link);
                // detailLinks.AddRange(links);
                houseList.AddRange(GetHouseList(links));
                //Thread.Sleep(1000);
            }
            houseList = houseList.Distinct().ToList();

            //HtmlAgilityPack.HtmlNode htmlnode = doc.DocumentNode.
            //   SelectSingleNode("//*[@id=\"container\"]/div[2]/div[1]/div[3]");
            ////*[@id="container"]/div[2]/div[1]/div[3]/div[1]
            ////List<string> detailLinks = new List<string>();
            //HashSet<string> detailLinks = new HashSet<string>();
            //foreach (HtmlAgilityPack.HtmlNode item in htmlnode.ChildNodes)
            //{
            //    if (item.NodeType.Equals(HtmlAgilityPack.HtmlNodeType.Element)
            //        && item.OriginalName.Equals("div"))
            //    {
            //        string detailLink = item.GetAttributeValue("data-link", string.Empty);
            //        if (!string.IsNullOrWhiteSpace(detailLink))
            //        {
            //            detailLinks.Add(detailLink);
            //        }
            //    }
            //    // sb.Append(string.Format("{0}:{1}\r\n", item.InnerText, item.Attributes["href"].Value));
            //}
            //var temp1 = "";
        }

        [TestMethod]
        public void Test1()
        {
            DirectoryInfo di = new DirectoryInfo(string.Format(@"{0}..\..\..\", System.AppDomain.CurrentDomain.BaseDirectory));
            var ssss = new DirectoryInfo(string.Format(@"{0}..\..\..\", System.AppDomain.CurrentDomain.BaseDirectory)).FullName;
            StringBuilder sb = new StringBuilder();
            foreach (var item in System.IO.File.ReadAllLines("D:\\temp.txt", Encoding.UTF8))
            {
                sb.AppendLine(ReplaceOhter(item));
            }
            var temp = sb.ToString();
        }

        [TestMethod]
        public void TestMethod1()
        {
            string str = "中铁建（北京）物业管理有限公司";
            Analyzer analyzer = new Lucene.China.ChineseAnalyzer();
            StringReader sr = new StringReader(str);
            TokenStream stream = analyzer.TokenStream(null, sr);
            long begin = System.DateTime.Now.Ticks;
            Token t = stream.Next();

            StringBuilder sb = new StringBuilder();
            sb.Remove(0, sb.Length);
            string t1 = "";
            int i = 0;

            while (t != null)
            {
                t1 = t.ToString();   //显示格式： (关键词,0,2) ，需要处理
                t1 = t1.Replace("(", "");
                char[] separator = { ',' };
                t1 = t1.Split(separator)[0];

                sb.Append(i + ":" + t1 + "\r\n");
                t = stream.Next();
                i++;
            }
            string temp = sb.ToString();

            //StringBuilder sb = new StringBuilder();
            //sb.Remove(0, sb.Length);
            //string t1 = "";
            //int i = 0;
            //Analyzer analyzer = new Lucene.China.ChineseAnalyzer();
            //StringReader sr = new StringReader(richTextBox1.Text);
            //TokenStream stream = analyzer.TokenStream(null, sr);

            //long begin = System.DateTime.Now.Ticks;
            //Token t = stream.Next();
            //while (t != null)
            //{
            //    t1 = t.ToString();   //显示格式： (关键词,0,2) ，需要处理
            //    t1 = t1.Replace("(", "");
            //    char[] separator = { ',' };
            //    t1 = t1.Split(separator)[0];

            //    sb.Append(i + ":" + t1 + "\r\n");
            //    t = stream.Next();
            //    i++;
            //}
            //richTextBox2.Text = sb.ToString();
            //long end = System.DateTime.Now.Ticks; //100毫微秒
            //int time = (int)((end - begin) / 10000); //ms

            //richTextBox2.Text += "耗时" + (time) + "ms \r\n=================================\r\n";
        }

        private List<string> GetDetailLinks(string url)
        {
            List<string> detailLinks = new List<string>();
            try
            {
                var html = HttpHelper.Request(url, null, RestSharp.Method.GET);
                HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
                doc.LoadHtml(html);
                HtmlAgilityPack.HtmlNode htmlnode = doc.DocumentNode.
                   SelectSingleNode("//*[@id=\"container\"]/div[2]/div[1]/div[3]");

                foreach (HtmlAgilityPack.HtmlNode item in htmlnode.ChildNodes)
                {
                    if (item.NodeType.Equals(HtmlAgilityPack.HtmlNodeType.Element)
                        && item.OriginalName.Equals("div"))
                    {
                        string detailLink = item.GetAttributeValue("data-link", string.Empty);
                        if (!string.IsNullOrWhiteSpace(detailLink))
                        {
                            detailLinks.Add(detailLink);
                        }
                    }
                }
                return detailLinks.Distinct().ToList();
            }
            catch
            {
                return detailLinks;
            }
        }

        private House GetHouse(string detailLink)
        {
            House house = new House();
            house.DetailLink = detailLink;

            try
            {
                var html = HttpHelper.Request(detailLink, null, RestSharp.Method.GET);
                HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
                doc.LoadHtml(html);

                //获取 楼盘详情 链接
                //有些楼盘上方有广告位 占据一个div，先从广告位位置取
                ////*[@id="header"]/div[4]
                var node = doc.DocumentNode.SelectSingleNode("//*[@id=\"header\"]/div[4]");
                if (node.GetAttributeValue("class", string.Empty).Equals("lp-toppic"))
                {
                    node = doc.DocumentNode.SelectSingleNode("//*[@id=\"header\"]/div[5]/div/ul/li[2]/a");
                }
                else
                {
                    node = doc.DocumentNode.SelectSingleNode("//*[@id=\"header\"]/div[4]/div/ul/li[2]/a");
                }

                var houseDetailLink = node.GetAttributeValue("href", string.Empty);
                if (string.IsNullOrWhiteSpace(houseDetailLink))
                {
                    return house;
                }

                html = HttpHelper.Request(houseDetailLink, null, RestSharp.Method.GET);
                doc = new HtmlAgilityPack.HtmlDocument();
                doc.LoadHtml(html);

                node = doc.DocumentNode.SelectSingleNode("//*[@id=\"container\"]/div[1]/div[1]/div[1]/div[2]/ul/li[1]/div[2]");
                house.Name = node.InnerText.Trim().Replace("  ", " ");

                node = doc.DocumentNode.SelectSingleNode("//*[@id=\"container\"]/div[1]/div[1]/div[1]/div[2]/ul/li[3]/div[2]/span");
                house.UnitPrice = node.InnerText.Trim();

                node = doc.DocumentNode.SelectSingleNode("//*[@id=\"container\"]/div[1]/div[1]/div[1]/div[2]/ul/li[6]/div[2]/a");
                house.DevelopersCorp = node.InnerText.Trim();

                node = doc.DocumentNode.SelectSingleNode("//*[@id=\"container\"]/div[1]/div[1]/div[3]/div[2]/ul/li[9]/div[2]/a");
                house.PropertyCorp = node.InnerText.Trim();
            }
            catch { }
            return house;
        }

        private List<House> GetHouseList(List<string> detailLinks)
        {
            List<House> list = new List<House>();
            foreach (var detailLink in detailLinks)
            {
                list.Add(GetHouse(detailLink));
            }
            return list.Distinct().ToList();
        }

        private string ReplaceOhter(string str)
        {
            var FilterKeywords = new List<string>();
            foreach (var line in File.ReadLines(BaseResource.FilterKeywordsFile))
            {
                FilterKeywords.Add(line);
            }
            foreach (var item in FilterKeywords)
            {
                str = str.Replace(item, string.Empty);
            }
            return str;
        }
    }
}